package com.yfqy.app.security;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GrantedAuthorityDeserializer extends JsonDeserializer<List<SimpleGrantedAuthority>> {

    @Override
    public List<SimpleGrantedAuthority> deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        JsonNode node = p.getCodec().readTree(p);

        if (node.isArray()) {
            for (JsonNode authNode : node) {
                if (authNode.has("authority")) {
                    String authority = authNode.get("authority").asText();
                    authorities.add(new SimpleGrantedAuthority(authority));
                } else if (authNode.isTextual()) {
                    authorities.add(new SimpleGrantedAuthority(authNode.asText()));
                }
            }
        }
        return authorities;
    }
}
